package com.vcredit.creditcat.time;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.threadpool.RequestLocalThread;
import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.enumtype.MobileStatusEnum;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.mobile.MobileProcessService;
import com.vcredit.bird.vo.mobile.MobileVarible;
import com.vcredit.creditcat.dto.mobile.MobileBillDetail;
import com.vcredit.creditcat.enumtype.PlatformTypeEnum;
import com.vcredit.creditcat.manager.TimeManager;
import com.vcredit.creditcat.model.AuthMobile;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.creditcat.util.EnumExtUtil;
import com.vcredit.framework.util.JacksonUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;

/**
 * 手机认证项状态查询后台任务
 */
@JobHander(value="mobileTime")
@Component
public class MobileTime extends IJobHandler {

	private static Logger log = LoggerFactory.getLogger(MobileTime.class);

	@Autowired
	private TimeManager<AuthMobile> timeManager;

	
	@Autowired private ThreadPoolTaskExecutor threadPoolTaskExecutor;
	 

	@Autowired
	private MobileProcessService mobileProcessService;

	@Override
	public ReturnT<String> execute(String... strings) throws Exception {
		log.info("手机认证定时任务执行了................" + ConcurrentDateUtil.formatDateTime(new Date()));
		RequestLocalThread.create();
		List<AuthMobile> list = null;
		try {
			list = timeManager.findUnCrawl(AuthMobile.class);
			if (list == null || list.isEmpty())
				return ReturnT.SUCCESS;
			for (AuthMobile obj : list) {
				threadPoolTaskExecutor.execute(new CrawleThread(obj));
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ReturnT.FAIL;
		}
		return ReturnT.SUCCESS;
	}

	class CrawleThread implements Runnable {
		private AuthMobile obj;
		private Logger logger = LoggerFactory.getLogger(CrawleThread.class);

		public CrawleThread(AuthMobile obj) {
			super();
			this.obj = obj;
		}

		public void run() {
			try {
				if (obj.getCust().getCustName() == null || obj.getCust().getCustIdno() == null) {
					return;
				}
				PlatformTypeEnum platformTypeEnum =
						EnumExtUtil.getEnumOnValue(PlatformTypeEnum.class,
								String.valueOf(obj.getPlatformType()), "code");
				BirdResponse ret = mobileProcessService.mobileCrawlerState(obj.getMobileId(), platformTypeEnum.getValue());
				MobileBillDetail result = JacksonUtil.Json2Obj(ret.getResult(), new TypeReference<MobileBillDetail>() {
				});
				if (result.getCode() == 201) {// 采集成功
					MobileVarible mobileVarible = mobileProcessService.mobileVaible(obj.getMobileId());
					String registerName;
					if (mobileVarible.getName().length() > 20) {
						registerName = mobileVarible.getName().substring(0, 20);
					} else {
						registerName = mobileVarible.getName();
					}
					obj.setRegisterName(registerName);
					String registerIdNo;
					if (mobileVarible.getIdentityCard().length() > 20) {
						registerIdNo = mobileVarible.getIdentityCard().substring(0, 20);
					} else {
						registerIdNo = mobileVarible.getIdentityCard();
					}
					obj.setRegisterIdNo(registerIdNo);
					MobileStatusEnum mobileStatusEnum = mobileProcessService.isReal(obj.getCust().getCustName(),
							obj.getCust().getCustIdno(), mobileVarible);
					obj.setResultStatus(mobileStatusEnum.getCode());
					if (MobileStatusEnum.REAL_NAME == mobileStatusEnum) {
						obj.setExpiredTime(ConcurrentDateUtil.formatDateTime(ConcurrentDateUtil.addDayTime(new Date(), 30)));
					}
					obj.setCity(mobileVarible.getCity());
					timeManager.updateCrawlerState(obj);
				} else if (result.getCode() == 202) { // 采集失败
					obj.setResultStatus(MobileStatusEnum.AUTHENTICATE_FAIL.getCode());
					timeManager.updateCrawlerState(obj);
				}
			} catch (Exception e) {
				logger.error("Exception occured", e);
			}
		}

		public int checkRealName(MobileVarible mobileVarible, Cust cust) {
			MobileStatusEnum mobileStatusEnum = mobileProcessService.isReal(cust.getCustName(), cust.getCustIdno(),
					mobileVarible);
			return mobileStatusEnum.getCode();
		}
	}
}
